package com.dayu.web.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.dayu.finecomm.protocol.pojo.TPacket;
import com.dayu.finecomm.protocol.pojo.TSensor;
import com.dayu.finecomm.protocol.pojo.TStation;
import com.dayu.web.param.SensorParam;
import com.dayu.web.pojo.Sensor;

/**
 * 站点service
 * 
 * @author Chenjiawei
 *
 *         2017年11月6日
 */
@Service
public class StationService {

	/**
	 * 获取所有站点
	 */
	public Map<String, List<String>> getStations(Map<String, TStation> map) {

		Iterator<TStation> ites = map.values().iterator();

		Map<String, List<String>> sm = new HashMap<String, List<String>>();

		while (ites.hasNext()) {
			TStation ts = ites.next();
			String name = ts.driveName;

			List<String> ll = sm.get(name);

			if (ll == null) {

				List<String> l = new ArrayList<String>();
				l.add(ts.packet.id());
				sm.put(name, l);
			} else {

				ll.add(ts.packet.id());
				sm.put(name, ll);
			}

		}
		stationSort(sm);

		return sm;
	}

	/**
	 * 获取当前新增的站点
	 */
	public Map<String, List<String>> getNewStations(
			Map<String, List<String>> old, Map<String, List<String>> curr) {

		Map<String, List<String>> n = new HashMap<String, List<String>>();
		Iterator<String> ite = curr.keySet().iterator();
		Set<String> oldName = old.keySet();

		List<String> oldS = null;
		List<String> currS = null;
		while (ite.hasNext()) {

			String currName = ite.next();

			if (oldName.contains(currName)) {

				oldS = old.get(currName);
				currS = curr.get(currName);

				for (String s : oldS) {

					if (currS.contains(s)) {
						currS.remove(s);
					}
				}
				if (currS.size() > 0) {
					n.put(currName, currS);
				}

			} else {
				n.put(currName, curr.get(currName));
			}
		}
		stationSort(n);

		return n;
	}

	/**
	 * 站点排序
	 */
	public Map<String, List<String>> stationSort(
			Map<String, List<String>> stations) {

		Iterator<String> ite = stations.keySet().iterator();

		String key = null;
		List<String> list = null;

		while (ite.hasNext()) {
			key = ite.next();
			list = stations.get(key);
			Collections.sort(list);
		}

		return stations;
	}

	/**
	 * 根据站点获取站号信息
	 */
	public List<Sensor> getSensors(SensorParam sernsParam,
			Map<String, TStation> map) {

		String station = sernsParam.getStation();
		String driveName = sernsParam.getDriveName();

		Iterator<TStation> ites = map.values().iterator();

		LinkedList<TSensor> tSensorlist = new LinkedList<TSensor>();

		Long time = null;
		Long updateTime = null;

		while (ites.hasNext()) {
			TStation ts = ites.next();

			if (ts.packet.id().equals(station)
					&& ts.driveName.equals(driveName)) {

				TPacket packet = ts.packet;
				tSensorlist = packet.sensors;
				time = packet.timestamp;
				updateTime = packet.created;
				break;
			}
		}

		List<Sensor> lists = new LinkedList<Sensor>();

		Map<String, Long> hmap = new HashMap<String, Long>();
		String sen = null;
		Long t = null;
		Long dataTime = null;
		for (TSensor s : tSensorlist) {

			sen = station + "_" + s.channel;
			dataTime = s.timestamp;
			if (dataTime == null) {
				dataTime = time;
			}

			t = hmap.get(sen);
			if (t == null) {
				hmap.put(sen, dataTime);

			} else if (t < dataTime) {
				hmap.put(sen, dataTime);
			}

		}

		for (TSensor s : tSensorlist) {

			sen = station + "_" + s.channel;
			dataTime = s.timestamp;
			if (dataTime == null) {
				dataTime = time;
			}

			t = hmap.get(sen);

			if (t <= dataTime) {

				t = dataTime;
				hmap.put(sen, t);

				Sensor sensor = new Sensor();
				sensor.setChannel(s.channel);
				sensor.setSn(sen);
				sensor.setValue(s.val.toString());

				sensor.setDataTime(dataTime);
				sensor.setUpdateTime(updateTime);

				lists.add(sensor);
			}
		}

		return lists;

	}
}
